// ELEC/XJEL2645
// Dr Craig A. Evans and Dr Tim J. Amsdon

#include "main.h"


void main_menu() {
  print_main_menu();
  int input = get_user_input(5);
  select_menu_item(input);
}

int get_user_input(int max_limit) {
  int input;
  std::string input_string;
  bool valid_input = false;

  do {
    std::cout << "\nSelect item: ";
    std::cin >> input_string;
    valid_input = is_integer(input_string);
    if (input_string.size() > 1) {
      valid_input = 0;
    }

    if (!valid_input) {
      std::cout << "Input one-digit integer!\n";
    } else { 
      input = std::stoi(input_string);  
      if (input >= 1 && input <= max_limit) {
        valid_input = true;
      } else {
        std::cout << "Invalid menu item!\n";
        valid_input = false;
      }
    }
  } while (!valid_input);

  return input;
}



void select_menu_item(int input) {
  switch (input) {
    case 1:
      menu_item_1();
      break;
    case 2:
      menu_item_2();
      break;
    case 3:
      menu_item_3();
      break;
    case 4:
      menu_help(); 
      break;
    case 5:
      std::cout << "Exiting program...\n";
      exit(0);  // Exit the program
      break;
    default:
      std::cout << "Invalid option. Please select a valid menu item.\n";
      break;
  }
}


void print_main_menu() {
    std::cout << "\n";
    std::cout << "******************************************\n";
    std::cout << "*           🌟 Electrical Master 🌟      *\n";
    std::cout << "******************************************\n";
    std::cout << "*                                        *\n";
    std::cout << "*        ███████╗        ███████╗        *\n";
    std::cout << "*        ██╔════╝        ██╔════╝        *\n";
    std::cout << "*        █████╗          █████╗          *\n";
    std::cout << "*        ██╔══╝          ██╔══╝          *\n";
    std::cout << "*        ███████╗        ███████╗        *\n";
    std::cout << "*        ╚══════╝        ╚══════╝        *\n";
    std::cout << "*                                        *\n";
    std::cout << "******************************************\n";
    std::cout << "*                                        *\n";
    std::cout << "*  1. 🔌 Rectifier Circuit Calculator    *\n";
    std::cout << "*  2. 🔄 Digital Signal Filter           *\n";
    std::cout << "*  3. ⚡ Boost-Buck Converter Calculator *\n";
    std::cout << "*  4. ❓ Help                            *\n";
    std::cout << "*  5. ❌ Exit                            *\n";
    std::cout << "*                                        *\n";
    std::cout << "******************************************\n";
    std::cout << "*            Designed by:                *\n";
    std::cout << "*    Weifangchuan, Dengjunhao, Xubohao   *\n";
    std::cout << "******************************************\n";
}







void go_back_to_main() {
  std::string input;
  do {
    std::cout << "\nEnter 'b' or 'B' to go back to main menu: ";
    std::cin >> input;
  } while (input != "b" && input != "B");
  main_menu();
}

// https://codereview.stackexchange.com/questions/162569/checking-if-each-char-in-a-string-is-a-decimal-digit
bool is_integer(std::string num) {
  return std::regex_match(num, std::regex("[+-]?[0-9]+"));
}

void menu_item_1() {
  std::cout << "\n>> Rectifier Circuit Calculator\n";
  std::cout << "\nSome code here does something useful\n";
  calculateMenu();
  go_back_to_main();
}

void menu_item_2() {
  std::cout << "\n>> Digital Signal Filter\n";
  filter_menu();
//  run_signal_generator(); 
//  process_signal();
  go_back_to_main();
  
}

void menu_item_3() {
  std::cout << "\n>> Boost-Buck Converter Calculator\n";
  displayMenu();
  go_back_to_main();
}


void menu_help() {
  std::cout << "\n>> Menu Help\n";
  std::cout << "\n1. 🔌 Rectifier Circuit Calculator\n";
  std::cout << "   - This calculator mainly focuses on dealing with the first step of mobile phone charger ---\n";
  std::cout << "     AC input and rectification. Through the user enter the AC input voltage and load resistance, \n";
  std::cout << "     such as load voltage, average current flowing through the load resistance and so on fundamental parameters' values in \n";
  std::cout << "     in half-wave/full-wave rectifier will be obtained and the system will complete the rectification. \n";
  std::cout << "\n2. 🔄 Digital Signal Filter\n";
  std::cout << "   - This option generates a raw signal with noise and applies filtering to simulate the filtering operation of a mobile phone charger.\n";
  std::cout << "     Users can input the signal sampling rate, raw signal frequency, noise amplitude, and noise frequency to generate the raw signal, \n";
  std::cout << "     which is then filtered using a first-order low-pass filter. \n";
  std::cout << "\n3. ⚡ Boost-Buck Converter Calculator\n";
  std::cout << "   - This option calculates key parameters for boost-buck converters, such as\n";
  std::cout << "     duty cycle, inductance, and capacitance, based on input and output voltages,\n";
  std::cout << "     ripple current/voltage, and frequency.\n";
  std::cout << "\n4. ❓ Help\n";
  std::cout << "   - Displays this help menu with information about each option.\n";
  std::cout << "\n5. ❌ Exit\n";
  std::cout << "   - Exits the program.\n";
  go_back_to_main();
}
